package org.daidai.abstrusewriting.homepage.classify.fragments;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import org.daidai.abstrusewriting.R;
import org.daidai.abstrusewriting.fragments.BaseFragment;
import org.daidai.abstrusewriting.homepage.classify.activitys.SpecialDetailActivity;
import org.daidai.abstrusewriting.homepage.classify.adapter.SpecialAdapter;
import org.daidai.abstrusewriting.homepage.classify.client.ClientAPI;
import org.daidai.abstrusewriting.homepage.classify.model.Special;
import org.daidai.abstrusewriting.homepage.classify.model.SpecialList;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class ClassifyItemFragment extends BaseFragment implements AdapterView.OnItemClickListener, SwipeRefreshLayout.OnRefreshListener, AbsListView.OnScrollListener {


    private View ret;
    private List<Special> mList;
    private ListView mListView;
    private SpecialAdapter mAdapter;
    private SwipeRefreshLayout mRefersh;
    private Bundle bundle;
    private int pageId=1;
    private boolean isRefashing=true;

    public ClassifyItemFragment() {
        // Required empty public constructor
    }

    @Override
    public String getFragmentTitle() {
        double random = Math.random();
        return "ClassifyItemFragment"+String.valueOf(random);
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        ret = inflater.inflate(R.layout.fragment_classify_item, container, false);
        if(!(EventBus.getDefault().isRegistered(this))){
            EventBus.getDefault().register(this);
        }

        bundle = getArguments();
        if(bundle !=null){
            int categoryId = bundle.getInt("categoryId");
            long keywordId = bundle.getLong("keywordId");

            // Toast.makeText(getContext(),String.valueOf(keywordId), Toast.LENGTH_SHORT).show();
            ClientAPI.getSpecialList(categoryId,keywordId,1,20);
        }

        mRefersh = (SwipeRefreshLayout) ret.findViewById(R.id.fragment_classify_item_refresh);
        if (mRefersh != null) {
            mRefersh.setOnRefreshListener(this);
            //设置刷新时动画的颜色，可以设置4个
            mRefersh.setColorSchemeResources(
                    android.R.color.holo_blue_light,
                    android.R.color.holo_red_light,
                    android.R.color.holo_orange_light,
                    android.R.color.holo_green_light
            );
        }



        mList = new ArrayList<>();
        mListView = (ListView) ret.findViewById(R.id.fragment_classify_item_listView);
        mAdapter = new SpecialAdapter(mList,getContext());
        mListView.setAdapter(mAdapter);
        mListView.setOnItemClickListener(this);

        mListView.setOnScrollListener(this);
        return ret;
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onGetSpecialList(SpecialList specialList){
        //Toast.makeText(getContext(), specialList.toString(), Toast.LENGTH_SHORT).show();
       if(pageId==1){
           mList.clear();
       }

        mList.addAll(specialList.getSpecials());
        mAdapter.addAll(specialList.getSpecials());
        isRefashing=false;
        mRefersh.setRefreshing(false);
    }

    @Override
    public void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent(getContext(), SpecialDetailActivity.class);
        Special special = mList.get(position);
        intent.putExtra("special",special);
        startActivity(intent);
    }

    @Override
    public void onRefresh() {
        if(bundle !=null){
            int categoryId = bundle.getInt("categoryId");
            long keywordId = bundle.getLong("keywordId");
            pageId=1;
            // Toast.makeText(getContext(),String.valueOf(keywordId), Toast.LENGTH_SHORT).show();
            ClientAPI.getSpecialList(categoryId,keywordId,1,20);

        }
    }




    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        switch (scrollState) {
            case SCROLL_STATE_IDLE:// 滑动停止时
                if (view.getLastVisiblePosition()==(view.getCount()-1)) {
                    if(!isRefashing){
                        pageId=pageId+1;
                        Toast.makeText(this.getContext(), "pageId:"+String.valueOf(pageId), Toast.LENGTH_SHORT).show();

                        if(bundle !=null){
                            int categoryId = bundle.getInt("categoryId");
                            long keywordId = bundle.getLong("keywordId");

                            // Toast.makeText(getContext(),String.valueOf(keywordId), Toast.LENGTH_SHORT).show();
                            ClientAPI.getSpecialList(categoryId,keywordId,pageId,20);

                        }



                        isRefashing=true;
                    }

                }

                break;
            case SCROLL_STATE_FLING://快速滑动时
                break;
            case SCROLL_STATE_TOUCH_SCROLL:

                break;
        }
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

    }
}
